package chu.dejian.entity;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sam.blemodule.R;
import com.sam.dejian.ble.wrist_bp.MeasureResult;
import com.sam.utils.L;

/**
 * Created by Administrator on 2015/12/12 0012.
 */
public class RecordEntity implements Parcelable {
    public int recordId;
    public int systolic, diastolic, pulse;
    public String dateTime;
    public String message,note;
    public int pulseState;
    public boolean isUploaded = false;

    public static final String RECORD_STORE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public RecordEntity(){
        recordId = systolic = diastolic = pulse = 0;
        dateTime = null;
    }

    public RecordEntity(int systolic, int diastolic, int pulse) {
        this.systolic = systolic;
        this.diastolic = diastolic;
        this.pulse = pulse;
        this.dateTime = new SimpleDateFormat(RECORD_STORE_TIME_FORMAT).format(new Date());
    }

    public RecordEntity(int systolic, int diastolic, int pulse, String dateTime) {
        this.systolic = systolic;
        this.diastolic = diastolic;
        this.pulse = pulse;
        this.dateTime = dateTime;
    }

    public static RecordEntity parseFromMeasureResult(Context context, MeasureResult measureResult){
        RecordEntity recordEntity = new RecordEntity(measureResult.highPressure,measureResult.lowPressure,measureResult.pulse);
        if(measureResult.highPressure > 140 || measureResult.lowPressure > 90){
            recordEntity.message = context.getString(R.string.high_blood_pressure) +"，";
        } else if(measureResult.highPressure < 90 || measureResult.lowPressure < 60){
            recordEntity.message = context.getString(R.string.low_blood_pressure) +"，";
        } else if(90 <= measureResult.highPressure && measureResult.highPressure <= 140 && 60 <= measureResult.lowPressure && measureResult.lowPressure <= 90){
            recordEntity.message = context.getString(R.string.normal_blood_pressure) +"，";
        } else {
            recordEntity.message = context.getString(R.string.abnormal_blood_pressure) +"，";
        }

        if(measureResult.isArrhythmia){
            recordEntity.pulseState = 1;
            recordEntity.message += context.getString(R.string.pulse_state_arrhythmia);
        } else {
            recordEntity.pulseState = 0;
            recordEntity.message += context.getString(R.string.pulse_state_normal);
        }
        recordEntity.message += "。";
        return recordEntity;
    }

    protected RecordEntity(Parcel in) {
        recordId = in.readInt();
        systolic = in.readInt();
        diastolic = in.readInt();
        pulse = in.readInt();
        dateTime = in.readString();
        message = in.readString();
        note = in.readString();
        pulseState = in.readInt();
        isUploaded = in.readByte() != 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(recordId);
        dest.writeInt(systolic);
        dest.writeInt(diastolic);
        dest.writeInt(pulse);
        dest.writeString(dateTime);
        dest.writeString(message);
        dest.writeString(note);
        dest.writeInt(pulseState);
        dest.writeByte((byte) (isUploaded ? 1 : 0));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<RecordEntity> CREATOR = new Creator<RecordEntity>() {
        @Override
        public RecordEntity createFromParcel(Parcel in) {
            return new RecordEntity(in);
        }

        @Override
        public RecordEntity[] newArray(int size) {
            return new RecordEntity[size];
        }
    };

    public Date getMeasureDateTime(){
        if(dateTime == null) return new Date();
        try {
            return new SimpleDateFormat(RECORD_STORE_TIME_FORMAT).parse(dateTime);
        } catch (ParseException e) {
            L.d("解析字符串时间到日期失败");
        }
        return new Date();
    }

    @Override
    public boolean equals(Object o) {
        if(o == null) return false;
        if(this == o) return true;
        if(o instanceof RecordEntity){
            RecordEntity re = (RecordEntity) o;
            return dateTime.equals(re.dateTime) && systolic == re.systolic
                    && diastolic == re.diastolic && pulse == re.pulse;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return dateTime.hashCode() + systolic + diastolic + pulse;
    }

    @Override
    public String toString() {
        return "RecordEntity{" +
                "recordId=" + recordId +
                ", systolic=" + systolic +
                ", diastolic=" + diastolic +
                ", pulse=" + pulse +
                ", dateTime='" + dateTime + '\'' +
                ", message='" + message + '\'' +
                ", note='" + note + '\'' +
                ", pulseState=" + pulseState +
                ", isUploaded=" + isUploaded +
                '}';
    }
}
